package com.hskn.hss.module.empPlan.vo;

import com.hskn.hss.module.alarmperson.entity.AlarmPerson;
import com.hskn.hss.module.depart.entity.SysDepart;
import com.hskn.hss.module.dlbjviolation.entity.DlbjViolation;
import com.hskn.hss.module.empPlan.entity.EmpPlan;
import com.hskn.hss.module.empPlanEmployee.entity.EmpPlanEmployee;
import com.hskn.hss.module.grid.entity.Grid;
import com.hskn.hss.module.rptEmployeeWork.entity.RptEmployeeWork;
import com.hskn.hss.module.tlemployee.entity.TlEmployee;
import com.hskn.hss.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 作业计划汇总报表TO
 *
 * @author zcy
 * @date 2021/10/9 13:21
 */
@Data
@ApiModel(value = "作业计划汇总报表列表-VO", description = "作业计划汇总报表列表-VO")
public class EmpPlanSummaryTableVO implements Serializable {
    @ApiModelProperty(value = "机构名称", name = "depName")
    private String depName;
    @ApiModelProperty(value = "网格名称", name = "gridName")
    private String gridName;
    @ApiModelProperty(value = "网格Id", name = "gridId")
    private String gridId;
    @ApiModelProperty(value = "计划名称", name = "planName")
    private String planName;
    @ApiModelProperty(value = "计划天数", name = "planDays")
    private Integer planDays;
    @ApiModelProperty(value = "完成天数", name = "overDays")
    private Integer overDays;
    @ApiModelProperty(value = "未完成天数", name = "unOverDays")
    private Integer unOverDays;
    @ApiModelProperty(value = "完成人数", name = "overPersons")
    private Integer overPersons;
    @ApiModelProperty(value = "报警次数", name = "alarmCounts")
    private Integer alarmCounts;
    @ApiModelProperty(value = "违规次数", name = "violationCounts")
    private Integer violationCounts;

    public static EmpPlanSummaryTableVO buildEmpPlanSummaryTableVO(
            Map<String, TlEmployee> employeeMap, Map<String, SysDepart> departMap, List<RptEmployeeWork> rptEmployeeWorkList,
            Map<String, List<AlarmPerson>> alarmMap, Map<String, List<DlbjViolation>> dlbjViolationMap, Map<String, Grid> gridMap) {
        EmpPlanSummaryTableVO empPlanSummaryTableVO = new EmpPlanSummaryTableVO();
        List<String> dateList = new ArrayList<>();
        String depName = "";
        String gridId = "";
        String gridName = "";
        String planName = "";
        Integer planDays = 0;
        Integer overDays = 0;
        Integer unOverDays = 0;
        Integer overPersons = 0;
        Integer alarmCounts = 0;
        Integer violationCounts = 0;
        if (CollectionUtils.isNotEmpty(rptEmployeeWorkList)) {
            depName = checkDepName( rptEmployeeWorkList ,  employeeMap , departMap);
        }
        if (StringUtils.isNotEmpty(checkPlanName(rptEmployeeWorkList))) {
            planName = checkPlanName(rptEmployeeWorkList);
        }

        planDays = Integer.parseInt(String.valueOf(rptEmployeeWorkList.stream().map(RptEmployeeWork::getWorkDate).distinct().count()));
        if (CollectionUtils.isNotEmpty(rptEmployeeWorkList)) {
            for (RptEmployeeWork rptEmployeeWork : rptEmployeeWorkList.stream().filter(rptEmployeeWork -> rptEmployeeWork.getStatus().equals(1)).collect(Collectors.toList())) {
                if (!dateList.contains(rptEmployeeWork.getWorkDate())) {
                    dateList.add(rptEmployeeWork.getWorkDate());
                }
            }
        }
        overDays = dateList.size();
        if (gridMap.containsKey(checkGridId(rptEmployeeWorkList))) {
            gridName = gridMap.get(checkGridId(rptEmployeeWorkList)).getName();
            gridId = checkGridId(rptEmployeeWorkList);
        }
        if (CollectionUtils.isNotEmpty(rptEmployeeWorkList)) {
            overPersons = Integer.parseInt(String.valueOf(rptEmployeeWorkList.stream().filter(rptEmployeeWork -> rptEmployeeWork.getStatus().equals(1)).count()));
        }
        if (alarmMap.containsKey(checkGridId(rptEmployeeWorkList))) {
            alarmCounts = alarmMap.get(checkGridId(rptEmployeeWorkList)).size();
        }
        if (dlbjViolationMap.containsKey(checkGridId(rptEmployeeWorkList))) {
            violationCounts = dlbjViolationMap.get(checkGridId(rptEmployeeWorkList)).size();
        }
        empPlanSummaryTableVO.setDepName(depName);
        empPlanSummaryTableVO.setGridId(gridId);
        empPlanSummaryTableVO.setGridName(gridName);
        empPlanSummaryTableVO.setPlanName(planName);
        empPlanSummaryTableVO.setPlanDays(planDays);
        empPlanSummaryTableVO.setOverDays(overDays);
        empPlanSummaryTableVO.setUnOverDays(planDays > 0 || overDays > 0 ? planDays - overDays : unOverDays);
        empPlanSummaryTableVO.setOverPersons(overPersons);
        empPlanSummaryTableVO.setAlarmCounts(alarmCounts);
        empPlanSummaryTableVO.setViolationCounts(violationCounts);
        return empPlanSummaryTableVO;
    }
    private static String checkDepName(List<RptEmployeeWork> rptEmployeeWorkList , Map<String, TlEmployee> employeeMap , Map<String, SysDepart> departMap){
        for (RptEmployeeWork empPlanEmployee : rptEmployeeWorkList) {
            String empId = empPlanEmployee.getEmployeeId();
            if (employeeMap.containsKey(empId)) {
                if (departMap.containsKey(employeeMap.get(empId).getEmpldeptid())) {
                    if (StringUtils.isNotEmpty(departMap.get(employeeMap.get(empId).getEmpldeptid()).getDepartName())) {
                        return departMap.get(employeeMap.get(empId).getEmpldeptid()).getDepartName();
                    }
                }
            }
        }
        return "";
    }
    private static String checkPlanName(List<RptEmployeeWork> rptEmployeeWorkList){
        for (RptEmployeeWork empPlanEmployee : rptEmployeeWorkList) {
            if (StringUtils.isNotEmpty(empPlanEmployee.getPlanName())) {
                return empPlanEmployee.getPlanName();
            }
        }
        return "";
    }
    private static String checkGridId(List<RptEmployeeWork> rptEmployeeWorkList){
        for (RptEmployeeWork empPlanEmployee : rptEmployeeWorkList) {
            if (StringUtils.isNotEmpty(empPlanEmployee.getGridId())) {
                return empPlanEmployee.getGridId();
            }
        }
        return "";
    }

}
